package com.slipper.ai.model.embedding.support;

import com.slipper.ai.model.embedding.EmbeddingModelBuilder;
import com.slipper.core.gson.GsonUtil;
import com.slipper.exception.CommonException;
import com.slipper.system.ai.bo.AiModelBO;
import org.springaicommunity.qianfan.QianFanEmbeddingModel;
import org.springaicommunity.qianfan.QianFanEmbeddingOptions;
import org.springaicommunity.qianfan.api.QianFanApi;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;

import java.util.Map;

public class QianFanEmbeddingModelBuilder implements EmbeddingModelBuilder {
    @Override
    public EmbeddingModel build(AiModelBO model) {
        Map<String, Object> options = GsonUtil.toMap(model.getParamJson());
        if (options == null || options.isEmpty() || !options.containsKey("apiUrl")) {
            throw new CommonException("参数配置不正确！");
        }

        String apiUrl = (String) options.get("apiUrl");
        String apiKey = (String) options.get("apiKey");
        String secretKey = (String) options.get("secretKey");
        QianFanApi qianFanApi = new QianFanApi(apiUrl, apiKey, secretKey);

        return new QianFanEmbeddingModel(
                qianFanApi,
                MetadataMode.EMBED,
                QianFanEmbeddingOptions.builder().model(model.getName()).build());
    }
}
